import os
from word_parser.pinyin_parser import PinyinParser
import argparse
from time import time

if __name__ == '__main__':
    argparser = argparse.ArgumentParser()
    argparser.add_argument('input', action='store', type=str, help='Input file path')
    argparser.add_argument('output', action='store', nargs='?', type=str, default='output.txt', help='Output file path')
    args = argparser.parse_args()

    word_parser = PinyinParser(3)
    word_parser.load('./')

    word_correct = 0
    sentence_correct = 0
    word_num = 0
    sentence_num = 0
    ans = []
    with open(args.input, 'r', encoding='utf-8') as f:
       line = f.readline().strip('\n').lower()
       chinese = f.readline().strip('\n')
       while line:
            print('输入的拼音：{}'.format(line),end='\t')
            start = time()
            res = word_parser.find_match(line)
            end = time()
            print('耗时: {:.2f} s'.format(end-start))
            ans.append(res)
            if res == chinese:
               sentence_correct += 1
            sentence_num += 1
            for word_res, word_ans in zip(res,chinese):
                if word_ans == word_res:
                    word_correct += 1
                word_num += 1
            line = f.readline().strip('\n').lower()
            chinese = f.readline().strip('\n')
    with open(args.output,'w',encoding='utf-8') as f:
        for res in ans:
            f.write(res)
    print('字准确率:{:.2%}, 句准确率:{:.2%}'.format(word_correct/word_num, sentence_correct/sentence_num))
    